(function () {
    /**
     * 排行榜弹窗
     * @class AlertRankUI
     * @constructor
     */
    function AlertRankUI() {
        AlertRankUI.super(this);

        this.rankRich.array  = [];
        this.rankDay.array   = [];
        this.rankWeek.array  = [];
        this.rankMonth.array = [];
        this.rankMine.array  = [];

        this.bindEvent();
    }
    Laya.class(AlertRankUI, "ui.Alert.Rank", ui.Alert.RankUI);

    var _proto = AlertRankUI.prototype;
    /**
     * @privite
     * 绑定相关事件
     */
    _proto.bindEvent = function () {
        //我的战绩-未登录，显示立即登录按钮，点击以后跳转到登录页
        this.btnLogin.on(Laya.Event.CLICK, this, function () {
            this.close("no_logged");
            location.href = Arena.LoginUrl;
        });

        //我的战绩-没有数据，显示立即游戏按钮，点击以后为快速开始游戏
        this.btnStartGame.on(Laya.Event.CLICK, this, function () {
            this.close("quick.start");
            viewer.publish("loader.load", "room");
        });

        //弹窗关闭回调函数
        this.closeHandler = new Laya.Handler(this, function (name) {
            this.btnRankTab.selectedIndex = -1;

            this.rankRich.array  = [];
            this.rankDay.array   = [];
            this.rankWeek.array  = [];
            this.rankMonth.array = [];
            this.rankMine.array  = [];

            this.rankNoLogin.visible     = false;
            this.rankNoRecord.visible    = false;
            this.rankMineHasData.visible = false;
        });

        //关联Tab与ViewStack
        this.btnRankTab.selectHandler = this.rankPage.setIndexHandler;

        //为List添加渲染回调
        this.rankRich.renderHandler  = Laya.Handler.create(this, this.onRichListRender, null, false);
        this.rankDay.renderHandler   = Laya.Handler.create(this, this.onRankListRender, null, false);
        this.rankWeek.renderHandler  = Laya.Handler.create(this, this.onRankListRender, null, false);
        this.rankMonth.renderHandler = Laya.Handler.create(this, this.onRankListRender, null, false);
        this.rankMine.renderHandler  = Laya.Handler.create(this, this.onRankMineRender, null, false);
        
        //注册排行榜相关IO事件
        viewer.register("rank::richlist", this.updateRich.bind(this));
        viewer.register("rank::ranklist", this.updateRank.bind(this));
        viewer.register("rank::myrecord", this.updateMine.bind(this));
    }
    /**
     * @privite
     * 富豪榜List渲染回调函数
     * 
     * @param {Object} item  当前的单元格对象
     * @param {Number} index 当前的单元格对象的索引值
     */
    _proto.onRichListRender = function (item, index) {
        var userName = item.getChildByName("nickname");

        userName.scrollRect = new Laya.Rectangle(0, 0, 130, 26);
    }
    /**
     * @privite
     * 我的战绩List渲染回调函数
     * 
     * @param {Object} item  当前的单元格对象
     * @param {Number} index 当前的单元格对象的索引值
     */
    _proto.onRankMineRender = function (item, index) {
        var mask = item.getChildByName("bg");
        
        if(index % 2 == 0){
            mask.visible = true;
        }else{
            mask.visible = false;
        }
    }
    /**
     * @privite
     * 日周月List渲染回调函数
     * 
     * @param {Object} item  当前的单元格对象
     * @param {Number} index 当前的单元格对象的索引值
     */
    _proto.onRankListRender = function (item, index) {
        var mask     = item.getChildByName("bg");
        var rankIcon = item.getChildByName("rankIcon");
        var name     = item.getChildByName("name");
        var amount   = item.getChildByName("amount");

        if(index < 3){
            rankIcon.visible = true;
            rankIcon.index   = index;
            name.color       = "#ffe213";
            amount.color     = "#ffe213";
        }else{
            rankIcon.visible = false;
            name.color       = "#ffdb9c";
            amount.color     = "#ffdb9c";
        }

        if(index % 2 === 0){
            mask.visible = true;
        }else{
            mask.visible = false;
        }
    }

    /**
     * @public
     * 更新富豪榜List数据
     * 
     * @param {Object} res 富豪榜相关数据
     */
    _proto.updateRich = function (res) {
        var richlist = res.result || [];

        for(var i = 0; i < richlist.length; i++){
            richlist[i].rank = i;
            richlist[i].nickname = richlist[i].user_name == "" ? richlist[i].user_id : utils.cutStr(richlist[i].user_name, 7);
            richlist[i].amount = richlist[i].total;
        }
        
        this.rankRich.array = richlist;
    }
    /**
     * @public
     * 更新日周月List数据
     * 
     * @param {Object} res 日周月排行榜相关数据
     */
    _proto.updateRank = function (res) {
        var data = res.data || [];

        for(var i in data){
            data[i].name   = utils.cutStr(data[i].nickname, 9);
            data[i].amount = data[i].amount | 0;
            data[i].trend  = (data[i].rank_trend | 0) - 1;
        }

        switch(res.type){
            case "day":
                this.rankDay.array = data;
                break;
            case "week":
                this.rankWeek.array = data;
                break;
            case "month":
                this.rankMonth.array = data;
                break;
        }

    }
    /**
     * @public
     * 更新我的战绩List数据
     * 
     * @param {Object} res 我的战绩相关数据
     */
    _proto.updateMine = function (res) {
        var myrecord = [];
        var uid = res.uid;
        
        if(uid == 0){
            this.rankNoLogin.visible     = true;

            this.rankNoRecord.visible    = false;
            this.rankMineHasData.visible = false;
        }else{
            myrecord = res.result;
            this.rankNoLogin.visible = false;

            if(myrecord.length === 0){
                this.rankNoRecord.visible    = true;
                this.rankMineHasData.visible = false;
            }else{
                for(var i in myrecord){
                    myrecord[i].time = myrecord[i].raw_add_time;
                    switch(myrecord[i].room_id){
                        case 1:
                            myrecord[i].room = "初级房";
                            break;
                        case 2:
                            myrecord[i].room = "中级房";
                            break;
                        case 3:
                            myrecord[i].room = "高级房";
                            break;
                            
                    }
                    myrecord[i].amount = myrecord[i].win_amount;
                }
                this.rankMine.array          = myrecord;
                this.rankNoRecord.visible    = false;
                this.rankMineHasData.visible = true;
            }
        }
    }
    /**
     * @public
     * 显示排行榜弹窗，发送相关获取数据的命令
     */
    _proto.display = function () {
        viewer.publish("io.emit", {
            cmd : "rank::richlist",
            params : {
                token : token
            }
        });

        viewer.publish("io.emit", {
            cmd : "rank::ranklist",
            params : {
                token : token,
                type : "day"
            }
        });

        viewer.publish("io.emit", {
            cmd : "rank::ranklist",
            params : {
                token : token,
                type : "week"
            }
        });

        viewer.publish("io.emit", {
            cmd : "rank::ranklist",
            params : {
                token : token,
                type : "month"
            }
        });

        viewer.publish("io.emit", {
            cmd : "rank::myrecord",
            params : {
                token : token
            }
        });

        this.popup();
    }
})();